# __author__  ==  WuJiaHao
import pygame
from pygame import gfxdraw
import config
from tkinter import messagebox
pygame.init()
# 版本号
versionId = "1.0"
pygame.display.set_caption(f"gameDemo -- 版本号:{versionId}")
screen = pygame.display.set_mode((900, 600))
running = True
# 棋子类
class Chess:
    def __init__(self,startColor,endColor,position):
        self.startColor = startColor
        self.endColor = endColor
        self.position = position
    # 渐变类型的棋子
    def draw(self,surface):
        base_radius = 15
        for r in range(base_radius, 0, -1):
            # 计算渐变颜色（从中心白色向边缘颜色过渡）
            alpha = int(255 * (r / base_radius))
            grad_color = (
                self.startColor[0] + int((self.endColor[0] - self.startColor[0]) * (1 - r / base_radius)),
                self.startColor[1] + int((self.endColor[1] - self.startColor[1]) * (1 - r / base_radius)),
                self.startColor[2] + int((self.endColor[2] - self.startColor[2]) * (1 - r / base_radius))
            )
            pygame.gfxdraw.aacircle(surface, self.position[0], self.position[1], r, grad_color)
            pygame.gfxdraw.filled_circle(surface, self.position[0], self.position[1], r, grad_color)
            surface.set_alpha(0)
def draw_board(interval=30):
    for i in range(interval+35,interval+35*15+1,35):
        pygame.draw.line(screen, (0, 0, 0), (i, interval+35), (i, interval+35*15), 1)
    for i in range(interval+35,interval+35*15+1,35):
        pygame.draw.line(screen, (0, 0, 0), (interval+35, i), (interval+35*15, i), 1)
    pygame.draw.circle(screen, (0, 0, 0), (interval+35*8, interval+35*8), 3)
    pygame.draw.circle(screen, (0, 0, 0), (interval+35*4, interval+35*4), 3)
    pygame.draw.circle(screen, (0, 0, 0), (interval+35*4, interval+35*12), 3)
    pygame.draw.circle(screen, (0, 0, 0), (interval+35*12, interval+35*4), 3)
    pygame.draw.circle(screen, (0, 0, 0), (interval+35*12, interval+35*12), 3)
def draw_text(texts,position,size=25,color = (80, 80, 80)):
    font = pygame.font.Font("font/font.ttf", size)
    text = font.render(texts, True, color)
    screen.blit(text, position)
def check_winner(board=config.saved_chess):
    # 检查横向、纵向、斜向是否有五子连珠
    for i in range(15):
        for j in range(11):
            if config.saved_chess[i][j] != 0 and config.saved_chess[i][j] == config.saved_chess[i][j + 1] == config.saved_chess[i][j + 2] == config.saved_chess[i][j + 3] == config.saved_chess[i][j + 4]:
                return config.saved_chess[i][j]
    for i in range(11):
        for j in range(15):
            if config.saved_chess[i][j]!= 0 and config.saved_chess[i][j] == config.saved_chess[i + 1][j] == config.saved_chess[i + 2][j] == config.saved_chess[i + 3][j] == config.saved_chess[i + 4][j]:
                return config.saved_chess[i][j]
    for i in range(11):
        for j in range(11):
            if config.saved_chess[i][j]!= 0 and config.saved_chess[i][j] == config.saved_chess[i + 1][j + 1] == config.saved_chess[i + 2][j + 2] == config.saved_chess[i + 3][j + 3] == config.saved_chess[i + 4][j + 4]:
                return config.saved_chess[i][j]
    for i in range(4, 15):
        for j in range(11):
            if config.saved_chess[i][j]!= 0 and config.saved_chess[i][j] == config.saved_chess[i - 1][j + 1] == config.saved_chess[i - 2][j + 2] == config.saved_chess[i - 3][j + 3] == config.saved_chess[i - 4][j + 4]:
                return config.saved_chess[i][j]
    return 0
def draw_winner(winner):
    if winner == 1:
        draw_text("黑棋胜利",(620,575),20,(0, 0, 0))
    elif winner == 2:
        draw_text("白棋胜利",(620,575),20,(0, 0, 0))
# 创建一个音频播放类
def Click_player():
    pygame.mixer.init()
    pygame.mixer.music.load("audio/bound.mp3")
    pygame.mixer.music.play()
while running:
    screen.fill((220, 220, 220))
    mouse_pos = pygame.mouse.get_pos()
    pygame.draw.rect(screen, (205, 133, 63), (45, 45, 530, 530),border_radius=20)
    pygame.draw.rect(screen, (55, 135, 35), (620, 45, 250, 271),border_radius=10)
    draw_board()
    check_winner()
    draw_winner(check_winner())
    if 65 <= mouse_pos[0] <= 555 and 65 <= mouse_pos[1] <= 555:
        GRID_SPACING = 35
        # 使用四舍五入计算列和行
        col = round((mouse_pos[0] - 65) / GRID_SPACING)
        row = round((mouse_pos[1] - 65) / GRID_SPACING)
        # 转换为整数并限制在0-14范围内（15x15棋盘）
        col = int(max(0, min(col, 14)))
        row = int(max(0, min(row, 14)))
        # 计算精确落子坐标
        x = 65 + col * GRID_SPACING
        y = 65 + row * GRID_SPACING
        # 导标数组
        config.gridPoints = [x,y]
        pygame.draw.circle(screen, (0, 0, 0), (x, y), 15, 1)
    draw_text("五子棋",(280,12),25)
    draw_text("第"+str(config.chessCount)+"手",(100,578),16)
    if config.chessCount % 2 == 0:
        draw_text("黑子执目",(160,578),16)
    else:
        draw_text("白子执目",(160,578),16)
    for i in range(15):
        for j in range(15):
            if config.saved_chess[i][j] == 1:
                black_chess = Chess((10, 10, 10), (180,180,180),[65+35*j,65+35*i])
                black_chess.draw(screen)
            elif config.saved_chess[i][j] == 2:
                white_chess = Chess((150, 150, 150),(255, 255, 255), [65+35*j,65+35*i])
                white_chess.draw(screen)
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1 and 65 <= mouse_pos[0] <= 555 and 65 <= mouse_pos[1] <= 555:
                GRID_SPACING = 35
                # 使用四舍五入计算列和行
                col = round((mouse_pos[0] - 65) / GRID_SPACING)
                row = round((mouse_pos[1] - 65) / GRID_SPACING)
                # 转换为整数并限制在0-14范围内（15x15棋盘）
                col = int(max(0, min(col, 14)))
                row = int(max(0, min(row, 14)))
                # 计算精确落子坐标
                x = 65 + col * GRID_SPACING
                y = 65 + row * GRID_SPACING
                if config.chessCount % 2 == 0 and [x, y] not in config.trajectoryArrayWhite and [x, y] not in config.trajectoryArrayBlack:
                    config.saved_chess[row][col] = 1
                    config.trajectoryArrayBlack.append([x, y])
                    config.chessCount += 1
                    Click_player()
                elif config.chessCount % 2 == 1 and [x, y] not in config.trajectoryArrayBlack and [x, y] not in config.trajectoryArrayWhite:
                    config.saved_chess[row][col] = 2
                    config.trajectoryArrayWhite.append([x, y])
                    config.chessCount += 1
                    Click_player()
        if event.type == pygame.QUIT:
            running = False
    pygame.display.flip()
